<template>
  <div class="home">
    <el-row style="width: 100%;height: 100%;">
      <el-col :span="16" class="left-box">
      </el-col>
      <el-col :span="8" class="right-box">
        <h1 class="content-title"> 航 空 售 票 管 理 系 统</h1>
        <h4 class="content-title">登 录</h4>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="70px" class="demo-ruleForm">
          <el-form-item label="账号" prop="account">
            <el-input v-model="ruleForm.account"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-link type="primary" href="/#/register">没有账号？前往注册</el-link>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        msg: 'Welcome to Your Vue.js App',
        ruleForm: {
          account: '',
          password: ''
        },
        rules: {
          account: [{
              required: true,
              message: '请输入账号',
              trigger: 'blur'
            },
            {
              min: 6,
              max: 10,
              message: '长度在 6 到 10 个字符',
              trigger: 'blur'
            }
          ],
          password: [{
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 8,
              max: 16,
              message: '密码长度在 8 到 16 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios.post("http://localhost:8888/login", this.ruleForm).then((data) => {
              if (data.data.length == 1) {
                this.$message({
                  message: '登录成功',
                  type: 'success'
                });
                this.$router.push({
                  path: '/index',
                  query: {
                    params: data.data[0]
                  }
                })
              } else {
                this.$message.error('登录失败！请检查账号密码');
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
  @import url("../css/common.css");

  .home {
    width: 100%;
    height: 100%;
  }

  .left-box {
    height: 100%;
    background: url(../img/login.jpg) repeat;
  }

  .right-box {
    height: 100%;
    text-align: center;
  }

  .content-title {
    color: #2CA9BB;
  }
</style>
